package com.haoji.upload.common.config.rocketmq;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * @program: upload
 * @description:
 * @author: xiaolei
 * @create: 2021-04-21 09:41
 **/
@Slf4j
@Configuration
public class DefaultProducerConfig {

    @Autowired
    private MqProperties mqProducerProperties;

    @Bean
    @Primary
    public DefaultMQProducer defaultProducer() throws MQClientException {
        DefaultMQProducer producer = new DefaultMQProducer(mqProducerProperties.getGroup());
        producer.setNamesrvAddr(mqProducerProperties.getNamesrvAddr());
        producer.setVipChannelEnabled(false);
        producer.setRetryTimesWhenSendAsyncFailed(10);
        producer.start();
        log.info("default producer 创建成功, {}, {}", mqProducerProperties.getNamesrvAddr(), mqProducerProperties.getGroup());
        return producer;
    }

}
